package com.fxnotes.database.inmemory.repository;

import com.google.inject.Inject;
import com.fxnotes.database.inmemory.InMemoryDatabase;
import com.fxnotes.domain.model.Book;
import com.fxnotes.domain.model.Chapter;
import com.fxnotes.domain.model.Page;
import com.fxnotes.domain.repository.IChapterRepository;
import java.util.List;

public class ChapterRepository implements IChapterRepository {

    private final InMemoryDatabase database;

    @Inject
    public ChapterRepository(InMemoryDatabase database) {
        this.database = database;
    }

    @Override
    public void add(Book book, Chapter chapter) throws EntityDuplicateException {
        if (book.getChapters().contains(chapter)) {
            throw new EntityDuplicateException("Chapter already exists!");
        }
        book.getChapters().add(chapter);
    }

    @Override
    public void updateName(Chapter chapter, String name) throws EntityDuplicateException {
        chapter.setName(name);
    }

    @Override
    public void updatePages(Chapter chapter, List<Page> pages) {
        chapter.getPages().clear();
        chapter.getPages().addAll(pages);
    }

    @Override
    public void remove(Book book, Chapter chapter) {
        book.getChapters().remove(chapter);
    }
}
